//
//  OrderObject.m
//  PurchasingMeeting
//
//  Created by zhuang yihang on 7/10/13.
//  Copyright (c) 2013 YX. All rights reserved.
//

#import "OrderObject.h"
#import "SizeObject.h"

@interface OrderObject(){
    
    NSString *pCode_;
    NSString *pColorCode_;
    
    eOrderStatus oStatus_;
    
    NSMutableDictionary *quantityDic_;
}

@end

@implementation OrderObject
@synthesize pColorCode = pColorCode_;
@synthesize pCode = pCode_;
@synthesize oStatus = oStatus_;
@synthesize quantity = quantityDic_;
@synthesize pParam;

- (id)initWithProduct:(ProductObject *)product{
    self = [super init];
    if (self) {
        pCode_ = product.pCode;
        pColorCode_ = product.pColorCode;
        oStatus_ = eOrderStatus_Waiting;
        quantityDic_ = [NSMutableDictionary dictionary];
        pParam = [NSMutableDictionary dictionary];
    }
    return self;
}
- (void)setOrder:(NSString *)sizeNo withQuantity:(int)quantity withShopID:(NSString *)shopID{
    NSLog(@"set order: %@,%@,%@,%d",pCode_,pColorCode_,sizeNo,quantity);
    [quantityDic_ setObject:[NSNumber numberWithInt:quantity] forKey:[NSString stringWithFormat:@"%@_%@",sizeNo,shopID]];
}

- (void)setOrderControl:(NSString *)sizeNo withShopID:(NSString *)shopID view:(UIView *)view{
    [pParam setObject:view forKey:[NSString stringWithFormat:@"%@_%@",sizeNo,shopID]];
}

- (int)getOrder:(NSString*)sizeNo withShopID:(NSString *)shopid{
    return [[quantityDic_ objectForKey:[NSString stringWithFormat:@"%@_%@",sizeNo,shopid]] intValue];
}

- (void)initializeFromDictionary:(NSDictionary *)dictionary{
    pCode_ = [dictionary objectForKey:@"product_code"];
    pColorCode_ = [dictionary objectForKey:@"product_color"];
    oStatus_ = [[dictionary objectForKey:@"order_status"] intValue];
    
    quantityDic_ = [dictionary objectForKey:@"order_quantity"];
}

- (NSDictionary *)transToDictionary{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    
    [dic setObject:pCode_ forKey:@"product_code"];
    [dic setObject:pColorCode_ forKey:@"product_color"];
    [dic setObject:[NSNumber numberWithInt:oStatus_] forKey:@"order_status"];
    [dic setObject:quantityDic_ forKey:@"order_quantity"];
    
    return dic;
}

- (int)getTotalOrderNumber{
    int total = 0;
    for ( NSNumber *number in quantityDic_.allValues ) {
        total += [number intValue];
    }
    
    return total;
}

- (int)getOrderNumberBySize:(NSArray *)array{
    int total = 0;
    for ( NSString *key in quantityDic_.allKeys ) {
        
        NSArray *comps = [key componentsSeparatedByString:@"_"];
        NSString *sID = [comps objectAtIndex:0];
        
        for (SizeObject *sizeNo in array) {
            if ([sID isEqualToString:sizeNo.sSizeNo]) {
                total += [[quantityDic_ objectForKey:key] intValue];
                break;
            }
        }
    }
    
    return total;
}

- (int)getOrderNumberInShop:(NSString *)shopID{
    int total = 0;
    for ( NSString *key in quantityDic_.allKeys ) {
        NSArray *comps = [key componentsSeparatedByString:@"_"];
        NSString *sID = [comps objectAtIndex:1];
        
        int val = [[quantityDic_ objectForKey:key] intValue];
        
        if ([sID isEqualToString:shopID]) {
            total += val;
        }
    }
    return total;
}


- (int)getOrderNumberBySeries:(NSString *)pSeries inShop:(NSString *)shop{
    return 0;
}


- (void)clearOrderBySize:(NSArray *)array{
    NSMutableArray *a = [NSMutableArray array];
    for ( NSString *key in quantityDic_.allKeys ) {
        
        NSArray *comps = [key componentsSeparatedByString:@"_"];
        NSString *sID = [comps objectAtIndex:0];
        
        for (SizeObject *size in array) {
            if ([sID isEqualToString:size.sSizeNo]) {
                [a addObject:key];
                break;
            }
        }
    }
    
    for (NSString *s in a) {
        [self clearkey:s];
    }
}

-(void)clearOrder
{
    for (NSString *s in quantityDic_.allKeys) {
        [self clearkey:s];
    }
}
-(void)clearkey:(NSString *)key
{
//    [quantityDic_ removeObjectForKey:key];
    [quantityDic_ setObject:@"0" forKey:key];
    
    id v = [pParam objectForKey:key];
    if ([v respondsToSelector:@selector(setText:)]) {
        [v performSelector:@selector(setText:) withObject:@"0"];
    }
}

@end
